define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/index' + location.search,
                    add_url: 'shop/add',
                    edit_url: 'shop/edit',
                    del_url: 'shop/del',
                    multi_url: 'shop/multi',
                    import_url: 'shop/import',
                    table: 'shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                // showToggle: false,
                // showColumns: false,
                // exportDataType: 'selected',
                exportTypes:['excel'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'shop_name', title: __('Shop_name'), operate: 'LIKE'},
                        {field: 'logo', title: __('店铺logo'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'description', title: __('Description'), operate: 'LIKE'},
                        {field: 'discount', title: __('Discount'), operate: 'LIKE'},
                        {field: 'shop_images', title: __('Shop_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'business_times', title: __('Business_times'), operate: 'LIKE'},
                        {field: 'label.name', title: __('Label.name'), operate: 'LIKE'},
                        {field: 'shop_address', title: __('Shop_address'), operate: 'LIKE'},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE'},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'view', title: __('浏览量'), operate: 'LIKE'},
                        {field: 'pay_num', title: __('成交量'), operate: 'LIKE'},
                        {field: 'hx_num', title: __('核销量'), operate: 'LIKE'},
                        {field: 'qrcode', title: __('店铺码图片'), operate: 'LIKE',events: Table.api.events.image, formatter: Table.api.formatter.image},
                        
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                       {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            
            
             //*************************** 自定义export开始
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                console.log(ids, page, all);
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("shop/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    //btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    btn: ["选中项(" + ids.length + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return false;
                    }
                })
            });
            //*************************** 自定义export结束

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
             bindevent: function () {
                $("#c-price,#c-is_verification,#c-is_signin").on("change keyup", function () {
                    var status = parseFloat($("#c-price").val()) > 0 || parseFloat($("#c-is_verification").val()) > 0 || parseFloat($("#c-is_signin").val()) > 0;
                    if (status) {
                        $("#c-is_need_login").val(1);
                        $("[data-input-id='c-is_need_login'] i").removeClass("fa-flip-horizontal text-gray").addClass("fa-toggle-on text-success");
                    }
                    $("[data-input-id='c-is_need_login']").toggleClass("disabled", status);
                });
                $("#c-is_signin").on("change", function () {
                    $("[data-type='signin']").toggleClass("hide", parseFloat($(this).val()) == 0);
                });
                $(document).on("click", "a[data-toggle='addresspicker']", function () {
                    if (!parseInt($(this).data("addon"))) {
                        Layer.alert("请在插件管理安装《地图位置(经纬度)选择》插件后重试");
                        return false;
                    }
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
